package com.taos.example;

import java.sql.*;

/**
 * @author PC
 */
public class RestQueryExample {
    public static void main(String[] args) throws SQLException {
        String jdbcUrl = "jdbc:TAOS-RS://localhost:6041/power?user=root&password=taosdata";
        try (Connection connection = DriverManager.getConnection(jdbcUrl)) {
            try (Statement stmt = connection.createStatement()) {
                ResultSet rs = stmt.executeQuery("SELECT AVG(voltage), location FROM meters GROUP BY location");

                ResultSetMetaData meta = rs.getMetaData();
                for (int i = 1; i <= meta.getColumnCount(); i++) {
                    String colLabel = meta.getColumnLabel(i);
                    System.out.print(colLabel);
                    System.out.print("\t");
                }
                System.out.println();

                while (rs.next()) {
                    for (int i = 1; i <= meta.getColumnCount(); i++) {
                        String value = rs.getString(i);
                        System.out.print(value);
                        System.out.print("\t");
                    }
                    System.out.println();
                }

                /**
                 * 输出结果：
                 * avg(voltage)	location
                 * 222.0	California.LosAngeles
                 * 219.0	California.SanFrancisco
                 */
            }
        }
    }
}
